﻿using MN.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.Notice
{
    public partial class NoticeMain : Form
    {
        MN.BLL.Notice bll_notice = new MN.BLL.Notice();
        List<NoticeModel> list;
        string UserId;
        public NoticeMain(string[] UserParam)
        {
            InitializeComponent();
            UserId = UserParam[0];
            BindControl();
            BindInfo();
        }

        private void BindControl()
        {
            list = new MN.BLL.Notice().GetList(0, 0, string.Format("where UserId = '{0}'", UserId));
            listBoxControl1.Items.Clear();
            foreach (var item in list)
            {
                listBoxControl1.Items.Add(item.Title);
            }
            listBoxControl1.SelectedIndex = 0;
        }

        private void BindInfo()
        {
            if (list == null || list.Count <= 0 || listBoxControl1.SelectedIndex < 0) return;
            NoticeModel notice = list[listBoxControl1.SelectedIndex];
            if (notice == null) return;
            txtTitle.Text = notice.Title;
            txtContent.Text = notice.Content;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (list == null || list.Count <= 0 || listBoxControl1.SelectedIndex < 0) return;
            NoticeModel notice = list[listBoxControl1.SelectedIndex];
            if (notice == null) return;
            notice.Title = txtTitle.Text;
            notice.Content = txtContent.Text;
            notice.UserId = this.UserId;
            notice.CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            notice.DataExist = true;
            notice.UserId = this.UserId;
            bll_notice.Update(notice);
            BindControl();
            BindInfo();
            MessageBox.Show("更新成功");
        }

        private void listBoxControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindInfo();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            NoticeModel notice = new NoticeModel();
            notice.Title = txtTitle.Text;
            notice.Content = txtContent.Text;
            notice.UserId = this.UserId;
            notice.CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            notice.DataExist = true;
            notice.UserId = this.UserId;
            bll_notice.Insert(notice);
            BindControl();
            BindInfo();
            MessageBox.Show("新增成功");
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定删除吗", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK) return;
            if (list == null || list.Count <= 0 || listBoxControl1.SelectedIndex < 0) return;
            NoticeModel notice = list[listBoxControl1.SelectedIndex];
            if (notice == null) return;
            bll_notice.Delete(notice.Pkid.ToString());
            BindControl();
            BindInfo();
            MessageBox.Show("删除成功");
        }
    }
}
